<?php
namespace Beerus\Modules\SellGoods\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Beerus\Modules\SellGoods\Actions\AuditFail;
use Beerus\Modules\SellGoods\Actions\AuditPass;
use Doraemon\model\Member;
use Doraemon\model\SellGoods\SellGoodsInviteRecord;
use Doraemon\model\SellGoods\SellGoodsTeam;
use Doraemon\service\SellGoods\InviteRecord;
use Doraemon\service\User;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Grid\Displayers\DropdownActions;
use Illuminate\Http\Request;

/**
 * 邀请记录管理
 */
class InviteRecordController extends Controller
{

    // 列表
    public function index(Request $request,Content $content) {
        $pageSize = $request->input("per_page", 20);
        $page = $request->input("page", 1);

        $params = $request->input();

        $search = [];
        if (!empty($params['uid'])) {
            $search['uid'] = $params['uid'];
        }
        if (!empty($params['nick'])) {
            $uid = User::getUidByNick($params['nick']);
            $search['uid'] = $uid;
        }

        // 状态
        $check_status = $params['check_status'] ? [$params['check_status']] : [];

        // 仅看通过
        $pass_status = !empty($params['is_passed'])? InviteRecord::PASS_STATUS : [];

        // 都是查询data_status字段
        if (!empty($check_status) && !empty($pass_status)) {
            $search['data_status'] = array_intersect($check_status, $pass_status) ?: [-1];
        } elseif (!empty($check_status) || !empty($pass_status)) {
            $search['data_status'] = array_merge($check_status, $pass_status);
        }

        if (!empty($search)) {
            $map['AND'] = $search;
        }
        $map['ORDER'] = ['created_at' => 'DESC'];
        $map['LIMIT'] = [
            ($page - 1) * $pageSize,
            $pageSize
        ];

        $total = 0;
        $record_list = SellGoodsInviteRecord::select('*', $map);
        if (!empty($record_list)) {
            $total = SellGoodsInviteRecord::count($search);
            $uids = array_column($record_list, 'uid');
            $member_list = Member::select(['uid', 'nick'], ['uid' => $uids]);
            $member_map = array_column($member_list, 'nick', 'uid');

            foreach ($record_list as $k => $v) {
                $record_list[$k]['nick'] = $member_map[$v['uid']] ?? '';
                $team = SellGoodsTeam::getById($v['team_id']);
                $record_list[$k]['team_name'] = $team ? $team['name'] : '';
                $record_list[$k]['status_text'] = SellGoodsInviteRecord::STATUS_TEXT[$v['data_status']] ?? '';
                $record_list[$k]['is_passed'] = in_array($v['data_status'], InviteRecord::PASS_STATUS) ? '通过' : '未通过';
            }
        }

        $gridModel = new pGridDataModel($record_list, $total, [
            'perPage' => $pageSize
        ]);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->setActionClass(DropdownActions::class);
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 3, function ($filter) {
                        $filter->equal('uid', '邀请人uid')->placeholder('请输入邀请人uid');
                    });
                    $filter->column(1 / 3, function ($filter) {
                        $filter->equal('nick', '邀请人站内昵称')->placeholder('请输入邀请人站内昵称');
                    });
                    $filter->column(1 / 3, function ($filter) {
                        $filter->equal('check_status', '状态')->select(SellGoodsInviteRecord::STATUS_TEXT);
                    });
                    $filter->column(1 / 3, function ($filter) {
                        $filter->equal('is_passed', '仅看通过')->checkbox([1 => '']);
                    });

                    $filter->expand();
                });

                $grid->column("id", 'ID');
                $grid->column("uid", '邀请人uid');
                $grid->column("nick", '邀请人站内昵称');
                $grid->column("team_name", '带货团队名称');
                $grid->column("name", '受邀人姓名');
                $grid->column("phone", '受邀人手机号');
                $grid->column("status_text", '状态');
                $grid->column("created_at", '邀请时间')->display(function () {
                    return $this->created_at->format('Y-m-d H:i:s');
                });
                $grid->column("updated_at", '加入时间')->display(function () {
                    if ($this->data_status == SellGoodsInviteRecord::STATUS_AUDIT_SUCCESS) {
                        return $this->updated_at->format('Y-m-d H:i:s');
                    }
                    return '/';
                });
                $grid->column("is_passed", '是否通过审核');

                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->disableView();

		            if ($actions->row->data_status == SellGoodsInviteRecord::STATUS_WAIT_AUDIT) {
			            $actions->add(new AuditFail);
			            $actions->add(new AuditPass);
		            }
                });
            });

        $tab = new Tab();
        $tab->add('邀请记录', $gridList->render());
        $content->row($tab);
        return $content
            ->header('邀请记录')
            ->description(' ')
            ->breadcrumb(['text' => '邀请记录']);
    }
}